<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCTlIOtun_JjZsZUMvOK0UT2yhxvFbtPIs&sensor=false">
</script>
<script type="text/javascript">
	var map;
	var geoCoder;
	var validLocation = 0;
	var hasInitLocation = {hasInitLocation};
	var isFixed = {isFixed};
	
	function initialize() {
		var mapOptions = {
			center: new google.maps.LatLng({latitude}, {longitude}),
			zoom: {zoom}, mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		geoCoder = new google.maps.Geocoder();
		map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
		if(isFixed != 1) {
			google.maps.event.addListener(map, 'click', function(event) {
				resolveCoordinates(event.latLng);
			});
		}
		if(hasInitLocation == 1) {
			marker = new google.maps.Marker({
				map: map,
				position: map.getCenter()
			});
			resolveCoordinates(map.getCenter());
		}
	}
	
	jQuery(document).ready(function() {
		initialize();
	});
	
	var marker = undefined;
	
	function findAddress() {
		var address = document.getElementById('mapSearch').value;
		geoCoder.geocode( { 'address': address}, function(results, status) {
			if(results.length == 0) {
				document.getElementById('currentLocationSelection').innerHTML = '<span style="color:#FF0000"><b>{i18n:script_itemAdd_noSearchResults}</b></span>';
				validLocation = 0;
				return false;
			}
			if(status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				if(typeof marker != "undefined") {
					marker.setMap(null);
				}
				marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location
				});
				map.setZoom(15);
				resolveCoordinates(results[0].geometry.location);
			} else {
				alert('An error occured while searching: ' + status);
			}
		});
		return false;
	}
	
	function resolveCoordinates(address) {
		geoCoder.geocode({'latLng': new google.maps.LatLng(address.lat(), address.lng())}, 
    		function(results, status) {
  				if(results.length == 0) {
					document.getElementById('currentLocationSelection').innerHTML = '<span style="color:#FF0000"><b>{i18n:script_itemAdd_noSearchResults}</b></span>';
					validLocation = 0;
					document.getElementById('latitude').value = '';
          			document.getElementById('longitude').value = '';
					return false;
				}
      			if(status == google.maps.GeocoderStatus.OK) {
        			if(results[0]) {
        				if(typeof marker != "undefined") {
							marker.setMap(null);
						}
						marker = new google.maps.Marker({
							map: map,
							position: new google.maps.LatLng(address.lat(), address.lng())
						});
          				document.getElementById('currentLocationSelection').innerHTML = '<b>' + results[0].formatted_address + '</b>';
          				validLocation = 1;
          				document.getElementById('latitude').value = address.lat();
          				document.getElementById('longitude').value = address.lng();
        			}
     			} else {
      				alert("An error occured while resolving the address: " + status);
				}
			});
	}
	
	function handleKeyPress(element, event) {
		var key = event.keyCode || event.which;
		if(key == 13){
			findAddress();
			return false;
		}
		return true;
	}
</script>
<div id="map_canvas" style="width:400px; height:300px;"></div>
<input type="hidden" id="latitude" name="latitude" value="">
<input type="hidden" id="longitude" name="longitude" value="">
<p>
	{i18n:itemAdd_dialogLocationSelection_yourSelection}: <span id="currentLocationSelection"></span>
</p>